﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BL.Model;
using BL.Model.Request;
using Common;
using DAL;
using DAL.Model;

namespace BL
{
    /// <summary>
    /// Controller for customer
    /// </summary>
    /// <history>
    /// 12/07/2014 phat.vo: Create new
    /// </history>
    public class CustomerController : BaseController, ICustomerController
    {
        readonly MasterEDM _masterEdm;

        #region Ctor

        public CustomerController(MasterSession masterEdm)
        {
            _masterEdm = masterEdm;
        }

        #endregion

        public void Create()
        {
            throw new NotImplementedException();
        }

        public void View()
        {
            throw new NotImplementedException();
        }

        public void Update()
        {
            throw new NotImplementedException();
        }

        public void Delete()
        {
            throw new NotImplementedException();
        }

        public void Print()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Search customer
        /// </summary>
        /// <param name="request"></param>
        /// <returns>List of customers</returns>
        /// <history>
        /// 12/07/2014 phat.vo: Create new
        /// </history>
        public List<Customer> Search(SearchingRequest request)
        {
            var isAllKeyword = string.IsNullOrWhiteSpace(request.Keyword);
            var keyword = isAllKeyword ? string.Empty : request.Keyword.Trim().ToLower();

            var query = from customer in _masterEdm.Customers
                        where (isAllKeyword ||
                               customer.ContactName.ToLower().Contains(keyword) ||
                               customer.Name.ToLower().Contains(keyword))
                        select customer;

            // Sorting
            switch (request.OrderBy)
            {
                case Ordering.SearchingCustomer.ContactName:
                    query = request.IsAsc
                        ? query.OrderBy(x => x.ContactName)
                        : query.OrderByDescending(x => x.ContactName);
                    break;
                default:
                    query = query.OrderBy(x => x.Name);
                    break;
            }

            if (request.IsPaging)
            {
                if (request.PageLength < 0)
                {
                    throw new Exception("PageLenght must be greater than 0");
                }
                if (request.StartIndex < 0)
                {
                    throw new Exception("StartIndex must be greater than 0");
                }
                query = query.Skip(request.StartIndex).Take(request.PageLength);
            }

            return query.ToList();
        }

        /// <summary>
        /// Search customer with default request. 
        /// Search only first page with default record
        /// </summary>
        /// <param name="keyword"></param>
        /// <returns>List of customers</returns>
        /// <history>
        /// 12/07/2014 phat.vo: Create new
        /// </history>
        public List<Customer> Search(string keyword)
        {
            return Search(new SearchingRequest
            {
                Keyword = keyword,
                PageLength = Constant.DefaultPageSize,
            });
        }
    }
}
